package com.peng._05weibo.controller;

import com.peng._05weibo.common.response.JsonResult;
import com.peng._05weibo.common.response.StatusCode;
import com.peng._05weibo.mapper.CommentMapper;
import com.peng._05weibo.pojo.dto.CommentDTO;
import com.peng._05weibo.pojo.entity.Comment;
import com.peng._05weibo.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * 评论控制器
 * @Version 1.0
 * @Author peng
 * @Date 2024-04-23 11:36
 */
@RestController
@RequestMapping("/v1/comment")
@Slf4j
@Api(tags="评论模块")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    /**
     * 发表评论
     * @param commentDTO 评论信息DTO
     * @param session 用户会话
     * @return 1-成功 2-用户未登录
     */
    @ApiOperation("发表评论")
    @PostMapping("/insert")
    public JsonResult addComment(@RequestBody CommentDTO commentDTO, HttpSession session){
        log.info("commentDTO:{}", commentDTO);
        UserVO userVO = (UserVO) session.getAttribute("user");
//        if (userVO == null){
//            System.out.println("用户未登录");
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }
        Comment comment = Comment.builder()
                .content(commentDTO.getContent())
                .created(new Date())
                .userId(userVO.getId())
                .weiboId(commentDTO.getWeiboId())
                .build();
        commentMapper.addComment(comment);
        System.out.println("评论成功");
        return JsonResult.success("评论成功");
    }

    /**
     * 根据微博id查询评论
     * @param id 微博id
     * @return 评论列表
     */
    @ApiOperation("根据微博id查询评论")
    @GetMapping("/selectByWeiboId")
    public JsonResult getCommentByWeiboId(Long id){
        return JsonResult.success("查询成功", commentMapper.selectByWeiboId(id));
    }
}
